<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Title</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <!-- 子组件给父组件传递数据 -->
    <!-- 父组件模板 -->
    <div id="app">
        <!-- 父组件监听子组件发射的自定义事件 -->
        <cpn @itemclick="cpnClick"></cpn>
    </div>




    <!-- 子组件模板 -->
    <template id="CPN">
        <div>
            <button v-for="item in categories" @click="btnClick(item)">{{item.name}}</button>
        </div>
    </template>

    <script>
        //子组件
        const cpn = ({
            template: '#CPN',
            data() {
                return {
                    categories: [{
                        id: 1,
                        name: '类目1'
                    }, {
                        id: 2,
                        name: '类目2'
                    }, {
                        id: 3,
                        name: '类目3'
                    }]
                }
            },
            props: {

            },
            methods: {
                btnClick(item) {
                    console.log("点击了:::" + item.name); //把点击了哪个类目传递给父组件
                    //子组件发射一个事件,父组件接受事件
                    this.$emit('itemclick', item)
                }
            }
        })



        //父组件
        const vm = new Vue({
            el: '#app',
            data: {
                message: '我是你爹!'
            },
            methods: {
                cpnClick(item) {
                    console.log('父组件::' + item.name);
                }
            },
            components: {
                cpn: cpn,
            }
        });
    </script>
</body>

</html>